<template>
  <el-dialog title="修改密码" :visible.sync="dialogVisible" width="30%">
    <!-- 表单 -->
    <el-form ref="ruleForm" :model="dataForm" :rules="dataRules" label-width="130px">
      <el-form-item label="重置密码" prop="password">
        <el-input v-model="dataForm.password" style="width:80%" show-password placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="再次输入密码" prop="passwordTry">
        <el-input v-model="dataForm.passwordTry" style="width:80%" show-password placeholder="请输入密码"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { confirm } from '@/config/publicMethod'
import { updateUserInfoRequest } from '@/api/other'
export default {
  data() {
    return {
      dialogVisible: false,
      title: '',
      dataForm: {
        password: '',
        passwordTry: '',
        id: '',
      },
      dataRules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码的长度在6~15个字符之间',
            trigger: 'blur',
          },
        ],
        passwordTry: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码的长度在6~15个字符之间',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    init(row) {
      this.dialogVisible = true
      this.dataForm.id = row.id
      this.$nextTick((_) => {
        this.$refs['ruleForm'].resetFields()
      })
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return
        const { password, passwordTry } = this.dataForm
        if (password !== passwordTry)
          return this.$message.info('两次输入密码不一致!')
        let data = {
          password: this.dataForm.password,
          id: this.dataForm.id,
        }
        const res = await confirm('确定修改该账号密码')
        if (res) {
          updateUserInfoRequest(data).then((res) => {
            const { code, msg } = res
            if (code !== '0000') return this.$message.error(msg)
            this.dialogVisible = false
            this.$message.success('修改成功')
            this.$emit('updata')
          })
        }
      })
    },
  },
}
</script>

<style>

</style>
